package com.alstudio.core.app;

import android.content.Context;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.app.VersionConfig;
import com.alstudio.utils.android.ALFileManager;
import com.alstudio.utils.android.device.ALDisplayMetricsManager;
import com.alstudio.utils.android.net.download.ALHttpDownloadListener;
import com.alstudio.utils.android.net.download.ALHttpDownloadManager;
import com.alstudio.utils.android.net.download.ALHttpDownloadTask;
import com.alstudio.utils.android.net.download.ALHttpDownloadTask.ALHttpDownloadErrorCode;
import com.alstudio.utils.log.ALLog;

public class ALSplashManager implements ALHttpDownloadListener {

	private static ALSplashManager mALSplashManager;

	private final String SUFFIX = "_tmp";
	private final String SPLASH_NAME = "splash";

	private boolean isDownloading = false;

	public static ALSplashManager getInstance() {
		if (mALSplashManager == null) {
			mALSplashManager = new ALSplashManager();
		}
		return mALSplashManager;
	}

	public String getSplashPath() {
		String path;
		path = ALLocalEnv.getLocalEnv().getSpManager()
				.getString("splash_path", "");
		if (!ALFileManager.isFileExsit(path)) {
			path = null;
		}
		ALLog.d("启动界面路径: " + path);
		return path;
	}

	public void setSplashPath(String path) {
		ALLocalEnv.getLocalEnv().getSpManager().putString("splash_path", path);
		ALLog.d("设置启动图片路径 " + path);
	}

	public void setSplashVersion(String ver) {
		ALLocalEnv.getLocalEnv().getSpManager().putString("splash_ver", ver);
		ALLog.d("设置启动图片版本 " + ver);
	}

	public String getSplashVersion() {
		String ver;
		ver = ALLocalEnv.getLocalEnv().getSpManager()
				.getString("splash_ver", "");
		return ver;
	}

	public boolean isNeedToUpdateSplashVersion(String ver) {
		String cur = getSplashVersion();
		String splashPath = getSplashPath();
		if (!cur.equals(ver) || !ALFileManager.isFileExsit(splashPath)) {
			setSplashVersion(ver);
			return true;
		}
		return false;
	}

	public synchronized void startDownloadSplash(Context context, String url) {

		if (isDownloading) {
			return;
		}
		isDownloading = true;

		// URL/平台/语言/980/640

		// 平台：android/ios
		// 语言：ch/fn/en/jp
		// 分辨率：980*640

		if (url.endsWith("/")) {
			url += "android/" + ALLocalEnv.getCurrentLanguage() + "/"
					+ ALDisplayMetricsManager.getScreenHeight(context) + "/"
					+ ALDisplayMetricsManager.getScreenWidth(context);
		} else {
			url += "/android/" + ALLocalEnv.getCurrentLanguage() + "/"
					+ ALDisplayMetricsManager.getScreenHeight(context) + "/"
					+ ALDisplayMetricsManager.getScreenWidth(context);
		}

		ALLog.d("启动界面下载URL： " + url);

		String path = VersionConfig.SPLASH_PATH + "/";
		String name = SPLASH_NAME;
		ALFileManager.deletFile(path + name);

		name += SUFFIX;

		ALHttpDownloadManager.startDownload(context, url, path, null, this);

	}

	@Override
	public void onUpdateProcess(ALHttpDownloadTask info) {
		// TODO Auto-generated method stub
		ALLog.d("下载进度: " + info.getDownloadPercent());
	}

	@Override
	public void onDownloadFinish(ALHttpDownloadTask info) {
		// TODO Auto-generated method stub
		info.getFilePath();
		isDownloading = false;
		ALLog.d("启动界面下载完成: " + info.getFilePath() + " 文件名: "
				+ info.getFileName());
		if (ALFileManager.renameFile(info.getFilePath(),
				VersionConfig.SPLASH_PATH + "/" + SPLASH_NAME)) {
			if (ALFileManager.isFileExsit(VersionConfig.SPLASH_PATH + "/"
					+ SPLASH_NAME)) {
				setSplashPath(VersionConfig.SPLASH_PATH + "/" + SPLASH_NAME);
			}
		}
	}

	@Override
	public void onPreDownload(String name) {
		// TODO Auto-generated method stub
		ALLog.d("准备开始下载");

	}

	@Override
	public void onDownloadFail(ALHttpDownloadErrorCode code, String name,
			ALHttpDownloadTask info) {
		// TODO Auto-generated method stub
		ALLog.e("下载失败:" + code);
		isDownloading = false;
		setSplashVersion("");
	}

}
